package blog.controller;

import blog.entity.ResultJson;
import blog.exception.ModelException;
import blog.exception.ResultJsonException;
import org.springframework.http.HttpStatus;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestController;

@RestController
@ControllerAdvice
public class ExceptionHandlerController{

    @ExceptionHandler(RuntimeException.class)
    public ResultJson runtimeExceptionHandler(RuntimeException e) {
        e.printStackTrace();
        return ResultJson.EXCEPTION;
    }

    @ExceptionHandler(NullPointerException.class)
    public ResultJson nullPointerExceptionHandler(NullPointerException e) {
        e.printStackTrace();
        return ResultJson.EXCEPTION;
    }

    @ExceptionHandler(InstantiationException.class)
    public ResultJson instantiationExceptionHandle(NullPointerException e) {
        e.printStackTrace();
        return ResultJson.EXCEPTION;
    }

    @ExceptionHandler(IllegalAccessException.class)
    @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
    public ResultJson illegalAccessExceptionHandle(IllegalAccessException e) {
        e.printStackTrace();
        return ResultJson.EXCEPTION;
    }

    @ExceptionHandler(ResultJsonException.class)
    @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
    public ResultJson ResultJsonExceptionHandle(ResultJsonException e) {
        e.printStackTrace();
        return e.getResultJson();
    }

    @ExceptionHandler(ModelException.class)
    public Model ModelExceptionHandle(ModelException e) {
        e.printStackTrace();
        return e.getModel();
    }
}
